yjx.directive('eSelect', ['$timeout', function eSelect($timeout) {
    return {
        replace: false,
        scope: {
            f: '=eFilter',       //单个过滤器
            t: '@eTitle',       //默认文字
            tn: '@eTnotlist',    //默认文字不做列表
            nf: '@eNofilter',   //不做过滤字段
            mod: '=ngModel',    //双向绑定  必须是{id:'',value:''}格式
            obj: '=eObject',    //绑定下拉列表对象 必须是[{id:'',value:''}]格式
            selecting: '=eFun' //点击事件方法
        },
        link: function ($scope, $element, attr) {
            $scope.createHtml = function () {
                var str = '';
                var obj = $scope.obj;
                var e = $element;
                var t = $scope.t;
                var tn = $scope.tn;
                var f = $scope.f,
                    nf = $scope.nf,
                    modvalue = $scope.mod ? $scope.mod.value : '';
                //console.info(f);
                //下拉菜单组装
                if (t) {
                    str += '<div class="select-title">' + t + '</div><div class="s-list"><ul class="e-hide">';
                    if (!tn && $scope.mod.value == '') {
                        str += '<li class="active" itemId=""  itemValue="">' + t + '</li>'
                    } else {
                        str += '<li itemId=""  itemValue="">' + t + '</li>'
                    }
                } else {
                    str += '<div class="select-title">' + obj[0].value + '</div><div class="s-list"><ul class="e-hide">';

                }
                ;
                var vv;
                //遍历拼接下拉菜单
                for (var i = 0; i < obj.length; i++) {
                    var v = obj[i].value,
                        id = obj[i].id;
                    //过滤器匹配，无过滤器也通过
                    if ((!f) || v.match(f) || nf == f) {
                        //绑定默认值
                        //console.info("绑定默认值"+$scope.mod.id)
                        if ($scope.mod && $scope.mod.id == id) {
                            vv = v;
                            e.find('.select-title').html(v);
                            //回传绑定值
                            $scope.mod.value = v;
                            str += '<li class="active" itemId=' + id + ' itemValue=' + v + '>' + v + '</li>';
                            continue;
                        } else {
                            if (!t && i == 0) {
                                //不存在默认值，默认选中第一个class="active"
                                //str += '<li  class="active" itemId=' + id + ' itemValue=' + v + '>' + v + '</li>'
                                str += '<li  itemId=' + id + ' itemValue=' + v + '>' + v + '</li>'
                            } else {
                                str += '<li  itemId=' + id + ' itemValue=' + v + '>' + v + '</li>'
                            }
                        }
                    }

                }
                str += '</ul></div>';
                e.html(str);
                $scope.bindEvent();
                e.find('.select-title').html(vv);//修改默认选中
            }
            //绑定点击事件
            $scope.bindEvent = function () {
                var e = $element;
                var l = e.find('li')
                l.bind('touchend', function touchs(event) {
                    //获取当前绑定标签
                    //l.parent().addClass('e-hide');
                    var li = $(event.target);
                    var vl = li.attr('itemValue');
                    var id = li.attr('itemId')
                    $timeout(function () {
                        //改变状态
                        l.removeClass('active');
                        li.addClass('active');
                        //回传绑定值必须直接获得变量值  不能写成$scope.mod={'id':id,'value':v}
                        if ($scope.mod) {
                            $scope.mod.id = id;
                            $scope.mod.value = vl;
                        }
                        $scope.selecting(id, vl);
                        //绑定默认值
                        //console.info(vl);
                        li.parent().parent().parent().find('.select-title').html(vl != '' ? vl : $scope.t);
                    });
                });
            }
            $scope.createHtml();
            $scope.$watch('f', function () {
                //判断过滤条件是否存在（不为空）则置空默认选项
                if ($scope.f != '' && $scope.mod)
                    $scope.mod.value = '';
                $scope.createHtml();
                return;
            });

            $scope.$watch('obj', function () {
                $scope.createHtml();
                return;
            });
            //var wF = $scope.$watch('f', function () {
            //    $scope.createHtml();
            //    wF();
            //    return;
            //})
            //var wObj = $scope.$watch('obj', function () {
            //    $scope.createHtml();
            //    wObj();
            //    return;
            //});
        }
    }
}])
//下拉选框展示隐藏
yjx.directive('selectingHide', ['$rootScope', function selectingHide($rootScope) {
    return {
        restrict: 'EAC',
        priority: 1,
        scope: false,
        link: function ($scope, $element, attr) {
            var p = $element.parent().parent();
            var c = $element.find('li');
            $element.bind('click', touchs);
            function touchs() {
                //鼠标点击区域为下拉选择框
                $rootScope.md = 1;
                if ($element.find('ul').attr('class') == 'e-hide') {
                    //初始化隐藏
                    angular.element(document.querySelector('.e-show')).addClass('e-hide')
                    angular.element(document.querySelector('.e-show')).removeClass('e-show')
                    /*因为认证老师处选择学校处添加*/
                    angular.element(document.querySelectorAll('.moreinfo')).removeClass('moreinfo');
                    $element.find('ul').addClass('e-show');
                    $element.find('ul').removeClass('e-hide');
                } else {
                    $element.find('ul').addClass('e-hide');
                    $element.find('ul').removeClass('e-show');
                }
                return;
            };
        }
    }
}])
    //外层点击隐藏
    .directive('ehideShow', ['$rootScope', '$timeout', function ehideShow($rootScope, $timeout) {
        return {
            restrict: 'EA',
            link: function ($scope, $element, attr) {
                //初始化鼠标点击区域 0外部 1下拉选择框
                $rootScope.md = 0;
                $element.bind('click', touchs);
                //$element.bind('touchend', touchs);
                //$element.bind('touchmove', touchs);
                function touchs() {
                    $timeout(function () {
                        if ($rootScope.md == 0) {
                            //若为外部则隐藏下拉选择框
                            angular.element(document.querySelectorAll('.e-show')).addClass('e-hide');
                            angular.element(document.querySelectorAll('.e-show')).removeClass('e-show');
                            /*因为认证老师处选择学校处添加*/
                            angular.element(document.querySelectorAll('.moreinfo')).removeClass('moreinfo');
                        } else {
                            //若为下拉框则重置鼠标点击区域
                            $rootScope.md = 0;
                        }
                    })
                }
            }
        }
    }])